home *** CD-ROM | disk | FTP | other *** search
- /*
- Test.java
-
- Test of JRI Native Methods.
-
- © 1997 by Michael J. Webb (mjw@codewell.com)
-
- */
-
- /* Mac Includes */
-
- #include <lowmem.h>
- #include <OSUtils.h>
-
- #include <stdio.h>
- #include <string.h>
-
- /* Java Runtime Headers */
- #include <NativeLibSupport.h>
- #include <jri.h>
-
- /* Java Class Name constant - change this for your methods. */
- #define kJRIClassName "TimeUtils"
-
- /* Forward declarations for our local native method implementation. */
-
- static long GetRealTime(JRIEnv* runtimeEnv, jref this);
- static long GetRelativeTime(JRIEnv* runtimeEnv, jref this);
- static JRIStringID GetTimeString(JRIEnv* runtimeEnv, jref this, jint someTime);
-
- /*
- Signature Summary. Remember these map to Java types, so (for example)
- 'Char' means a unicode character, 'Int' means long.
-
- Single character codes:
-
- Boolean Z
- Byte B
- Char C
- Double D
- Float F
- Int I
- Long J
- Short S
- Void V (Only as a return type)
-
- Special codes:
-
- Array [ followed by one of the other types
- Object L followed by the package of the class terminated by ';'
-
- The method name array is a NULL terminated array of
- methodname(signature)returntype strings.
-
- Some examples:
-
- void foo();
- "foo()V"
-
- int foo();
- "foo()I"
-
- String foo();
- "foo()Ljava/lang/String;"
-
- void foo(String bar);
- "foo(Ljava/lang/String;)V"
-
- void foo(byte[]);
- "foo([B)V"
-
- int foo(int a, int b, int c);
- "foo(III)I"
-
- void foo(String[] a, int[][] b)
- "foo(Ljava/lang/String;[[I)V"
-
- */
-
- /* Array of Native Method signatures. */
- static char* methodNames[] =
- {
- "GetRealTime()I",
- "GetRelativeTime()I",
- "GetTimeString(I)Ljava/lang/String;",
- NULL /* NULL terminates the array. */
- };
-
- /*
- The procedure array is a NULL terminated array of procedure
- pointers in the same order as the method signature array.
- */
-
- /* Array of Native Method implementations. */
- static void* methodProcedures[] =
- {
- GetRealTime,
- GetRelativeTime,
- GetTimeString,
- NULL /* NULL terminates the array. */
- };
-
- /* A native method to get the current time. */
- long GetRealTime(JRIEnv* runtimeEnv, jref this)
- {
- long realTime = 0;
-
- runtimeEnv = runtimeEnv;
- this = this;
-
- GetDateTime(((unsigned long*)(&realTime)));
-
- return realTime;
- }
-
- /* A native method to get the tick count. */
- long GetRelativeTime(JRIEnv* runtimeEnv, jref this)
- {
- SInt32 relTime = 0;
-
- runtimeEnv = runtimeEnv;
- this = this;
-
- relTime = LMGetTicks();
-
- return relTime;
- }
-
- /* A native method to convert an integer to a time string. */
- static JRIStringID GetTimeString(JRIEnv* runtimeEnv, jref this, jint someTime)
- {
- JRIStringID newString;
- DateTimeRec dateTime;
- char buffer[256];
-
- SecondsToDate(someTime, &dateTime);
-
- sprintf
- (
- buffer,
- "%d/%d/%d %d:%d:%d",
- dateTime.day,
- dateTime.month,
- dateTime.year,
- dateTime.hour,
- dateTime.minute,
- dateTime.second
- );
-
- newString = (runtimeEnv->interface)->NewStringUTF(runtimeEnv, buffer, strlen(buffer));
-
- return newString;
- }
-
- /*
- Initialization routine for these native methods. It will be
- called at load library time.
-
- This implementation is pretty generic; if you change the #define of
- 'kJRIClassName' above for you native methods, this routine should
- work.
- */
- OSStatus
- InitJavaLibrary
- (
- JRIRuntimeInstance* runtimeInstance,
- CFragConnectionID javaFragment
- )
- {
- OSStatus initResult = noErr;
- JRIEnv* runtimeEnv = NULL;
- JRIClassID classID = NULL;
-
- /* Try to initialize the support library. */
- initResult = InitNativeLibSupport(runtimeInstance, javaFragment);
-
- /* Now, register our native methods. */
- if (initResult == noErr)
- {
- /* Create a new Java runtime environment. */
- runtimeEnv = JRI_NewEnv(runtimeInstance, NULL);
-
- if (runtimeEnv != NULL)
- {
- /* Get a cookie for the class whose methods we implement. */
- classID = JRI_FindClass(runtimeEnv, kJRIClassName);
-
- if (classID == nil)
- {
- initResult = fnfErr;
- }
- else
- {
- /* Register the methods. */
- JRI_RegisterNatives
- (
- runtimeEnv,
- classID,
- methodNames,
- methodProcedures
- );
- }
-
- /* Clean up the environment we used. */
- JRI_DisposeEnv(runtimeInstance, runtimeEnv);
- }
- }
-
- return initResult;
- }
-